package com.qianguo.buyer.address;

import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.baidu.location.Poi;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseFragment;
import com.qianguo.buyer.entity.BuyerAddress;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;


/**
 * 添加收货地址
 *
 * @author eva
 * @date 16/8/17
 */
@EFragment(R.layout.layout_buyer_add_address)
public class BuyerAddAddressFragment extends BaseFragment {

    @ViewById(R.id.buyer_address_receiver_edit_text)
    EditText mEdtReceiver;

    @ViewById(R.id.buyer_address_mobile_edit_text)
    EditText mEdtMobile;

    @ViewById(R.id.buyer_address_edit_text)
    EditText mEdtAddress;

    @ViewById(R.id.buyer_address_type_grid_view)
    GridView mAddressType;

    @ViewById(R.id.buyer_address_save_button)
    Button mBtnSave;

    private AddressTypeAdapter mAdapter;

    @Override
    protected void initializeUI() {
        final BuyerAddress.AddressType[] types = BuyerAddress.AddressType.values();
        mAdapter = new AddressTypeAdapter(types);
        mAddressType.setNumColumns(types.length);
        mAddressType.setAdapter(mAdapter);
        mAddressType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mAdapter.setSelectedPosition(position);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Click(R.id.buyer_address_save_button)
    void clickSave() {
        String receiver = mEdtReceiver.getText().toString();
        String mobile = mEdtMobile.getText().toString();
        String address = mEdtAddress.getText().toString();
        if (validateInfo(receiver, mobile, address)) {
            mBtnSave.setBackgroundResource(R.drawable.shape_accent_color_solid_5_corner);
            saveAddress(receiver, mobile, address, mAdapter.getItem(mAdapter.selectedPosition).getId());
        }
    }

    private boolean validateInfo(String receiver, String mobile, String address) {
        if (TextUtils.isEmpty(receiver) || TextUtils.isEmpty(mobile) || TextUtils.isEmpty(address)) {
            return false;
        }
        return true;
    }

    @Background
    void saveAddress(String receiver, String mobile, String address, int addressTypeId) {
        BuyerAddress buyerAddress = new BuyerAddress();
        buyerAddress.setReceiver(receiver);
        buyerAddress.setMobile(mobile);
        buyerAddress.setAddress(address);
        buyerAddress.setAddType(addressTypeId);
        Poi poi = mApp.getPoi();
        if (poi != null) {
            buyerAddress.setPoiId(Integer.valueOf(poi.getId()));
            buyerAddress.setPoiName(poi.getName());
        }
        mApp.getApi().addAddress(mApp.getUser().getToken(), buyerAddress, new HttpRequestCallback<String>() {
            @Override
            public void onSuccess(int code, String msg, String data) {
                getActivity().finish();
                EventBus.getDefault().post(new AddAddressEvent(data));
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    private class AddressTypeAdapter extends BaseAdapter {
        BuyerAddress.AddressType[] types;
        private int selectedPosition = 0;
        public AddressTypeAdapter(BuyerAddress.AddressType[] types) {
            this.types = types;
        }

        @Override
        public int getCount() {
            return types == null ? 0 : types.length;
        }

        @Override
        public BuyerAddress.AddressType getItem(int position) {
            return types[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView type = new TextView(getActivity());
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(120, ViewGroup.LayoutParams.WRAP_CONTENT);
            type.setLayoutParams(lp);
            type.setPadding(12, 8, 12, 8);
            type.setGravity(Gravity.CENTER);
            if (position == selectedPosition) {
                type.setBackgroundResource(R.drawable.shape_accent_color_hollow_5_corner);
                type.setTextColor(getResources().getColor(R.color.accent_color));
            } else {
                type.setBackgroundResource(R.drawable.shape_gray_color_hollow_5_corner);
                type.setTextColor(getResources().getColor(R.color.light_grey));
            }
            type.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.sp_10));
            final BuyerAddress.AddressType item = getItem(position);
            type.setText(item.toString());

            return type;
        }

        public void setSelectedPosition(int position) {
            this.selectedPosition = position;
        }
    }
}
